/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.dusa.worklog.search;

import cz.dusa.worklog.controller.WorklogDataManager;
import cz.dusa.worklog.datalib.Project;
import cz.dusa.worklog.viewer.WorklogWindowManager;
import java.util.List;
import org.netbeans.spi.quicksearch.SearchProvider;
import org.netbeans.spi.quicksearch.SearchRequest;
import org.netbeans.spi.quicksearch.SearchResponse;

public class ProjectSearch implements SearchProvider {

    /**
     * Method is called by infrastructure when search operation was requested.
     * Implementors should evaluate given request and fill response object with
     * apropriate results
     *
     * @param request Search request object that contains information what to search for
     * @param response Search response object that stores search results. Note that it's important to react to return value of SearchResponse.addResult(...) method and stop computation if false value is returned.
     */
    @Override
    public void evaluate(SearchRequest request, SearchResponse response) {
        List<Project> customers = WorklogDataManager.getInstance().getAllProjects();
        for (Project p : customers) {
            if (p.getName().toLowerCase().contains(request.getText().toLowerCase())) {
                if (!response.addResult(new OpenProject(p), p.getName())) {
        	      break;
        	  }
            }
        }
    }

    private static class OpenProject implements Runnable {
        private Project project;

        public OpenProject(Project project) {
            this.project = project;
        }

        public void run() {
            WorklogWindowManager.getInstance().openWindow(project);
        }
    }
}
